package com.imcrl.manager
{
	import com.imcrl.core.IFactory;
	import com.imcrl.core.IPoolable;
	
	/**
	 * 对像池 
	 * @author crl
	 * 
	 */	
	public class Pool
	{
		private var list:Vector.<IPoolable>;
		private var max:int;
		
		private var factory:IFactory;
		public function Pool(factory:IFactory)
		{
			list=new Vector.<IPoolable>();
			
			this.factory=factory;
		}
		
		public function set maxSize(value:int):void{
			this.max=value;
			
		}
		
		public function get maxSize():int{
			return this.max;
		}
		
		public function add(value:IPoolable):Boolean{
			if(list.length==max){
				value.pool=null;
				value.dispose();
				return false;
			}
			value.sleep();
			list.push(value);
			
			return true;
		}
		
		public function get():IPoolable{
			var item:IPoolable;
			if(list.length){
				item=list.pop();
				item.awaken();
				return item;
			}
			item=factory.newInstance();
			item.pool=this;
			return item;
		}
	}
}